package com.gcd.tempe;

import java.text.BreakIterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.gcd.tempe.util.GlobalVariabel;
import com.gcd.tempe.util.JSON_Response;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TableLayout.LayoutParams;

public class MemesanBarangActivity extends Activity {
	public JSONArray jasaPengiriman;
	public int shippingID = -1;
	EditText addressEditText;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pesanbarang);
        addressEditText = new EditText(this);
        if(Integer.parseInt(GlobalVariabel.userLoginInfo.point) < 1000) {
        	//Ceklis pesan pakai poin  && teks total poin diilangin 
        	//Menu Login diilangin
        	View b1 = findViewById(R.id.textView1);
        	b1.setVisibility(View.GONE);
        	
        	View b2 = findViewById(R.id.checkUsePoint);
        	b2.setVisibility(View.GONE);
        } else {
        	//Teks total poin diupdate
        	TextView tv1 = (TextView) findViewById(R.id.textView1);
        	tv1.setText(tv1.getText()+" "+GlobalVariabel.userLoginInfo.point);
        }
        
        //Checkbox jasa pengiriman listener
        CheckBox checkJasaPengiriman = ( CheckBox ) findViewById( R.id.checkJasaPengiriman);
        checkJasaPengiriman.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                	try {
						setJasaPengirimanContent();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                } else {
                	//Remove Content:
                	RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroupJasaPengiriman);
                	rg.removeAllViews();
                	shippingID = -1;
                }
            }
        });
        
        jasaPengiriman = JSON_Response.getJasaPengiriman();
    }
	
	
	public void setJasaPengirimanContent() throws JSONException{
		LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        llp.gravity =  Gravity.CENTER;
        
		addressEditText.setText("Alamat");
		addressEditText.setGravity(Gravity.CENTER);
		addressEditText.setLayoutParams(llp);
		
		RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroupJasaPengiriman);
		rg.addView(addressEditText);
		
		final RadioButton[] rb = new RadioButton[jasaPengiriman.length()];
		rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
		for (int i = 0; i < jasaPengiriman.length(); i++) {
			final JSONObject thisJasaPengiriman = jasaPengiriman.getJSONObject(i);
			
			rb[i]  = new RadioButton(this);
	        rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
	        rb[i].setText(" "+thisJasaPengiriman.getString("originCity")+"-"+thisJasaPengiriman.getString("destinationCity")+" ("+thisJasaPengiriman.getString("qos")+")-"+thisJasaPengiriman.getString("rate")+"/kg");
	        rb[i].setTextSize(13);
	        rb[i].setTextColor(Color.BLACK);
	        rb[i].setId(thisJasaPengiriman.getInt("shippingServiceId"));
	    }
		
		//Set listener
	    shippingID = rg.getCheckedRadioButtonId();
	    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
	      public void onCheckedChanged(RadioGroup arg0, int id) {
	    	  shippingID = id;
	      }
	      
	    });
	}
	
	public void buttonOrderClick(View v){
		final CheckBox checkBox = (CheckBox) findViewById(R.id.checkUsePoint);
		final CheckBox checkBoxJasaPengiriman = (CheckBox) findViewById(R.id.checkJasaPengiriman); 
		String statusOrder = "-1";
		
		String address = addressEditText.getText().toString();
        if (checkBox.isChecked()) {
        	if (checkBoxJasaPengiriman.isChecked()) { //pake poin + pake jasa kirim
        		if (shippingID != -1) 
        			statusOrder = GlobalVariabel.cartItems.orderItemsInItemsCart(true, true, address, Integer.toString(shippingID));
        		else { 
        			Toast.makeText(MemesanBarangActivity.this, "Anda belum memilih jasa pengiriman", Toast.LENGTH_SHORT).show();
        			return;
        		}
        	} else { //pake poin + gak pake jasa kirim
        		statusOrder = GlobalVariabel.cartItems.orderItemsInItemsCart(true, false, address, Integer.toString(shippingID));
        	}
        } else {
        	if (checkBoxJasaPengiriman.isChecked()) { //gak pake poin + pake jasa kirim
        		if (shippingID != -1) 
        			statusOrder = GlobalVariabel.cartItems.orderItemsInItemsCart(false, true, address, Integer.toString(shippingID) );
        		else { 
        			Toast.makeText(MemesanBarangActivity.this, "Anda belum memilih jasa pengiriman", Toast.LENGTH_SHORT).show();
        			return;
        		}
        	} else { //gak pake poin + gak pake jasa kirim
        		statusOrder = GlobalVariabel.cartItems.orderItemsInItemsCart(false, false, address, Integer.toString(shippingID));
        	}
        }
		
    	int statusOrderInt;
    	try {
    		statusOrderInt = Integer.parseInt(statusOrder);
    	} catch (NumberFormatException e) {
    		statusOrderInt = -1;
		}
    	Log.d("Order", statusOrder);
    	if (statusOrderInt!=-1) {
    		Toast.makeText(MemesanBarangActivity.this,"Barang telah dipesan dengan sukses!",Toast.LENGTH_SHORT).show();
    		GlobalVariabel.cartItems.clearCart();
    		
    		//Back to main menu :
    		Intent intent = new Intent(MemesanBarangActivity.this, MainTEMPE.class);
    		startActivity(intent);
    	} else {
    		Toast.makeText(MemesanBarangActivity.this,"Barang gagal dipesan!",Toast.LENGTH_SHORT).show();
    	} 
		
		Intent intent = new Intent(MemesanBarangActivity.this, MainTEMPE.class);
		startActivity(intent);
    }
}
